home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / c / SUPRALib.lha / SUPRALib / Developer / Source.ORG / FCopy.c < prev    next >
C/C++ Source or Header  |  1999-05-17  |  4KB  |  131 lines

  1. /****** FCopy **************************************************************
  2. *
  3. *   NAME
  4. *       FCopy -- copies source file to destination file (V10)
  5. *       (dos V36)
  6. *
  7. *       Please use FCopyTags() instead.
  8. *
  9. *   SYNOPSIS
  10. *       error = FCopy(source, dest, buffer)
  11. *
  12. *       UBYTE = FCopy(char *, char *, LONG);
  13. *
  14. *   FUNCTION
  15. *       This function works very similar to C:Copy program. It copies
  16. *       a source file to a destination file.
  17. *       Please see more powerful FCopyTags() function (asynchronous).
  18. *
  19. *   INPUTS
  20. *       source - pointer to a source file name (with a relative or
  21. *                absolute path)
  22. *       dest - pointer to a destination file name
  23. *       buffer - maximum size of a buffer (in bytes) to be
  24. *                allocated for copying. If this buffer is 0, FCopy()
  25. *                will try to allocate buffer a size of a source file,
  26. *                or the largest memory block available. (this is the
  27. *                fastest way).
  28. *
  29. *   RESULT
  30. *       error - zero if no error. Function may return one of the
  31. *       following error definitions:
  32. *
  33. *           FC_ERR_EXIST - Source file does not exist
  34. *           FC_ERR_EXAM  - Error during examination of a source file
  35. *           FC_ERR_MEM   - Not enough memory availabe
  36. *           FC_ERR_OPEN  - Source file could not be oppened
  37. *           FC_ERR_READ  - Error while reading a source file
  38. *           FC_ERR_DIR   - Source file path is a directory
  39. *           FC_ERR_DEST  - Destination file could not be created
  40. *           FC_ERR_WRITE - Error while writing to a destination file
  41. *
  42. *   EXAMPLE
  43. *
  44. *       \* This example will copy a file c:dir to ram: with a new name
  45. *        * list.
  46. *        *\
  47. *
  48. *       UBYTE err;
  49. *
  50. *       if ((err = FCopy("C:Dir", "ram:list", 0)) == 0) {
  51. *
  52. *           no errors...
  53. *
  54. *       } else {
  55. *           printf("Error: %d\n", err); \* Error occured during FCopy() *\
  56. *
  57. *       }
  58. *
  59. *   NOTES
  60. *       If an error occurs then a destination file will not be deleted
  61. *       if it has already been partly copied.
  62. *
  63. ************************************************************************/
  64.  
  65. #include <exec/memory.h>
  66. #include <proto/dos.h>
  67. #include <proto/exec.h>
  68. #include <libraries/supra.h>
  69.  
  70.  
  71. UBYTE FCopy(char *source, char *dest, LONG buf)
  72. {
  73. struct FileInfoBlock fib;
  74.  
  75. LONG fsize, max;  /* part = One part of buffer */
  76. APTR mem=NULL;
  77. LONG len;
  78. BPTR lock,fsource=NULL,fdest=NULL;
  79. UBYTE err=0;
  80.  
  81.     if ((lock = Lock(source, ACCESS_READ)) == NULL) return(FC_ERR_EXIST);
  82.     if (Examine(lock, &fib) == NULL) {
  83.         UnLock(lock);
  84.         return(FC_ERR_EXAM);
  85.     }
  86.  
  87.     if (fib.fib_DirEntryType < 0) {
  88.         fsize = fib.fib_Size;
  89.  
  90.         if (fdest = Open(dest, MODE_NEWFILE)) {
  91.  
  92.             if (buf == 0) max = AvailMem(MEMF_LARGEST);
  93.             else max = buf;
  94.  
  95.             if (max > fsize) max = fsize;
  96.  
  97.             /* Allocate a buffer */
  98.             while ((mem = AllocMem(max, 0L)) == NULL && max > 1024) max-=1024;
  99.  
  100.             if (mem) {
  101.  
  102.               if (fsource = OpenFromLock(lock)) {
  103.                   /* Do the copying process */
  104.  
  105.                   do {
  106.                       len = Read(fsource, mem, max);
  107.                       if (len == -1) {
  108.                           err = FC_ERR_READ;
  109.                           break;
  110.                       } else if (len == 0) break;
  111.  
  112.                        if ((LONG) Write(fdest, mem, len) != len) {
  113.                           err = FC_ERR_WRITE;
  114.                           break;
  115.                       }
  116.                   } while (TRUE);
  117.               } else err = FC_ERR_OPEN; /* if OpenFromLock */
  118.             } else err = FC_ERR_MEM; /* Not enough memory */
  119.         } else err = FC_ERR_DEST;   /* if Destination File Open */
  120.     } else err = FC_ERR_DIR;    /* If source is dir */
  121.  
  122.     if (fsource) Close(fsource);
  123.     if (fdest) Close(fdest);
  124.     if (lock) UnLock(lock);
  125.     if (mem) FreeMem(mem, max);
  126.  
  127.     return(err);
  128. }
  129.  
  130.  
  131.